package com.xiaoxin.experience.service.hls.common;

import com.xiaoxin.experience.util.MD5;
import com.xiaoxin.experience.util.StringUtil;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xiaoxin
 */
@Setter
@Getter
public class HlsTask implements Serializable
{
    private static final long serialVersionUID = -2568569781955241727L;

    public HlsTask()
    {

    }

    public HlsTask(String name, String savePath, String downloadUrl)
    {
        this.taskId = MD5.md5(downloadUrl);
        this.name = name;
        this.savePath = savePath;
        this.downloadUrl = downloadUrl;
    }

    private String taskId;

    private String name;

    private String savePath;

    private String downloadUrl;

    /**
     * 0: 初始化
     * 1: 正在运行
     * 2: 已完成
     * 4: 异常
     */
    private int status = 0;

    private String spendTime;

    private String fileSize;

    private String progress = "0.0";

    private int totalFileCount;

    private AtomicInteger finishFileCount = new AtomicInteger(0);

    private String taskRecord;

    private String fileSavePath;

    private boolean removed;

    private AtomicInteger failFileCount = new AtomicInteger(0);

    private String failRecord;

    public String getTaskRecord()
    {
        if (totalFileCount == 0)
        {
            return null;
        }
        return finishFileCount.get() + "/" + totalFileCount;
    }

    public String getFailRecord()
    {
        if (totalFileCount == 0)
        {
            return null;
        }
        return failFileCount.get() + "/" + totalFileCount;
    }

    public String getProgress()
    {
        if (totalFileCount == 0)
        {
            return progress;
        }
        return StringUtil.floatFormat((finishFileCount.get()*100.0)/totalFileCount);
    }
}
